package com.project.app.server.pay;

import com.project.common.core.domain.AjaxResult;
import com.project.common.core.domain.entity.SysUser;
import com.project.common.core.domain.model.LoginUser;
import com.project.common.core.page.TableDataInfo;
import com.project.common.utils.SecurityUtils;
import com.project.server.domain.visitorRecord.TVisitorRecord;
import com.project.wxServer.domain.pay.WxPayInfo;
import com.project.wxServer.server.pay.IPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static com.project.wxServer.server.pay.impl.PayServiceImpl.setXml;

/**
 * @ClassName: WXPayController
 * @Author: goln
 * @Date: 2021/8/9　14:25
 * @Description：支付物业费controller
 */
@RestController
@RequestMapping("/pay")
public class WXPayController {

    private static final Logger logger = LoggerFactory.getLogger(WXPayController.class);

    @Resource
    private IPayService payService;

    /**
     * 物业缴费
     *
     * @param wxPayInfo
     * @return
     */
    @PostMapping("/payment")
    public AjaxResult payment(@RequestBody WxPayInfo wxPayInfo, HttpServletRequest request) {
        Long userId = 0l;
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SysUser user = loginUser.getUser();
            userId = user.getUserId();
        } catch (Exception e) {
            return AjaxResult.error("请重新登录");
        }
        wxPayInfo.setUserId(userId);
        return payService.payPrepay(wxPayInfo, request);
    }

    /**
     * 微信订单回调接口
     *
     * @return
     */
    @PostMapping("/notify")
    public void wxNotify(HttpServletRequest request, HttpServletResponse response) {
        //完成订单
        payService.wxNotify(request, response);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        try {
            response.getWriter().write(setXml("SUCCESS", "OK"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 轮询支付结果
     */
    @PostMapping("/payresult")
    public AjaxResult payresult(@RequestBody WxPayInfo wxPayInfo){
        return AjaxResult.success(payService.getPayResult(wxPayInfo));
    }


    /**
     * 完成订单
     *
     * @param tOrderRecord
     * @return

     @Transactional public TOrderRecord finishOrder(TOrderRecord tOrderRecord) {
     TOrderRecord finishtOrderRecord = null;
     try {
     //订单状态如果成功，则做后续业务，否则只留记录不作后续业务
     if (tOrderRecord != null) {
     //面对面缴费需要拆分记录
     if (tOrderRecord.getCommodityType() == CommodityType.FACETOFACE_PAY.getValue()) {
     FaceToFace faceToFace = (FaceToFace) faceToPayHashMap.get(faceToPayOrderId);
     itSaleRechargeRecordService.faceToOrderRecord(faceToFace, tOrderRecord);
     } else if (tOrderRecord.getCommodityType() == CommodityType.ORDER_FORM.getValue()) {
     List<WxPayInfo> wxPayInfoFinish = (List<WxPayInfo>) orderPayHashMap.get(orderPayOrderId);
     itSaleRechargeRecordService.orderPayRecord(wxPayInfoFinish, tOrderRecord);
     } else if (tOrderRecord.getCommodityType() == CommodityType.WITHHOLD_PAY.getValue()) {
     List<WxPayInfo> wxPayInfoFinish = (List<WxPayInfo>) withholdHashMap.get(withholdOrderId);
     itSaleRechargeRecordService.orderPayRecord(wxPayInfoFinish, tOrderRecord);
     } else {
     itSaleRechargeRecordService.createOrderRecord(tOrderRecord);
     }
     }
     if ((tOrderRecord != null) && (tOrderRecord.getOrderStatus().equals("1"))) {
     //订单充值
     if (tOrderRecord.getCommodityType() == CommodityType.PROMPTLY_PAY.getValue()) {
     //修改余额(仅限充值服务)
     payService.balanceCompute(tOrderRecord.getCommodityType(), tOrderRecord.getUserId(), tOrderRecord.getPaymentAmount());
     }
     //临时缴费
     if (tOrderRecord.getCommodityType() == CommodityType.WITHHOLD_PAY.getValue()) {
     List<WxPayInfo> wxPayInfoFinish = (List<WxPayInfo>) withholdHashMap.get(withholdOrderId);
     for (WxPayInfo wxPayInfo : wxPayInfoFinish) {
     Long recordId = wxPayInfo.getRecordId();
     TOperateParkingRecord tOperateParkingRecord = itOperateParkingRecordService.selectTOperateParkingRecordById(recordId);
     tOperateParkingRecord.setPayStatus("1");
     itOperateParkingRecordService.updateTOperateParkingRecord(tOperateParkingRecord);
     }
     withholdHashMap.remove(withholdOrderId);
     }
     //订单缴费
     if (tOrderRecord.getCommodityType() == CommodityType.ORDER_FORM.getValue()) {
     List<WxPayInfo> wxPayInfoFinish = (List<WxPayInfo>) orderPayHashMap.get(orderPayOrderId);
     for (WxPayInfo wxPayInfo : wxPayInfoFinish) {
     Long recordId = wxPayInfo.getRecordId();
     TOperateParkingRecord tOperateParkingRecord = itOperateParkingRecordService.selectTOperateParkingRecordById(recordId);
     tOperateParkingRecord.setPayStatus("1");
     itOperateParkingRecordService.updateTOperateParkingRecord(tOperateParkingRecord);
     }
     orderPayHashMap.remove(orderPayOrderId);
     }
     //车位租赁
     if (tOrderRecord.getCommodityType() == CommodityType.CARRENTAL_PAY.getValue()) {
     //生成支付记录标志完成租赁
     CarRental carRentals = (CarRental) carRentalPayHashMap.get(carRentalPayOrderId);
     tSaleLeaseRecordService.createRentalRecord(carRentals);
     carRentalPayHashMap.remove(carRentalPayOrderId);
     }
     //现场支付
     if (tOrderRecord.getCommodityType() == CommodityType.ON_PAYMENT.getValue()) {
     itOperateParkingRecordService.payTheFees(withholdHashMap, withholdOrderId);
     withholdHashMap.remove(withholdOrderId);
     }
     //面对面缴费
     if (tOrderRecord.getCommodityType() == CommodityType.FACETOFACE_PAY.getValue()) {
     itOperateParkingRecordService.faceToPayTheFees(faceToPayHashMap, faceToPayOrderId);
     faceToPayHashMap.remove(faceToPayOrderId);
     }
     //完成订单
     finishtOrderRecord = itOrderRecordService.updateWXOrder(tOrderRecord.getOrderStatus(), tOrderRecord.getCommodityId());
     }
     } catch (Exception e) {
     logger.error(e.getMessage());
     }
     return finishtOrderRecord;
     }*/
}
